/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gameheart.transport;

import gameheart.server.*;
import java.io.*;
import java.util.logging.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
import org.xml.sax.*;

/**
 *
 * @author win7
 */
public class XmlProcessor {

    public Command parseMessage(String message) throws NumberFormatException, IOException, DOMException {
        InputSource inputSource = new InputSource();
        inputSource.setCharacterStream(new StringReader(message));
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        Document doc = null;
        DocumentBuilder docBuilder = null;
        Element root = null;
        int commandId = -1;
        try {
            docBuilder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException parseEx) {
            parseEx.printStackTrace();
        }
        try {
            doc = docBuilder.parse(inputSource);
            root = doc.getDocumentElement();
            Element comIdElement = (Element) root.getElementsByTagName(CommandMessageTag.CommandIdTag).item(0);
            if (comIdElement != null) {
                commandId = Integer.valueOf(comIdElement.getTextContent());
            }
        } catch (SAXException saxEx) {
            saxEx.printStackTrace();
        }
        Element agrumentElement = (Element) root.getElementsByTagName(CommandMessageTag.ArgumentTag).item(0);
        Command command = new Command();
        command.setCommandId(commandId);

        NodeList argumentElements = agrumentElement.getChildNodes();
        int argNumber = argumentElements.getLength();
        String[] argNames = new String[argNumber];
        String[] argValues = new String[argNumber];
        for (int index = 0; index < argNumber; ++index) {
            argNames[index] = ((Element) argumentElements.item(index)).getTagName();
            argValues[index] = ((Element) argumentElements.item(index)).getTextContent();
        }
        command.setArgNumber(argNumber);
        command.setArgNames(argNames);
        command.setArgdValues(argValues);
        return command;
    }

    public String convertToXmlString(Command command) throws TransformerFactoryConfigurationError, IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = null;
        try {
            docBuilder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(NetworkTransporter.class.getName()).log(Level.SEVERE, null, ex);
        }
        Document doc = docBuilder.newDocument();
        Element root = doc.createElement(CommandMessageTag.CommandTag);
        doc.appendChild(root);

        Element commandIdElement = doc.createElement(CommandMessageTag.CommandIdTag);
        commandIdElement.appendChild(doc.createTextNode(String.valueOf(command.getCommandId())));
        root.appendChild(commandIdElement);
        Element agrumentsElement = doc.createElement(CommandMessageTag.ArgumentTag);
        int argNumber = command.getArgNumber();
        String[] argName = command.getArgNames();
        String[] argValue = command.getArgdValues();
        for (int index = 0; index < argNumber; ++index) {
            Element argumentElement = doc.createElement(argName[index]);
            argumentElement.setTextContent(argValue[index]);
            agrumentsElement.appendChild(argumentElement);
        }
        root.appendChild(agrumentsElement);
        TransformerFactory tranFactory = TransformerFactory.newInstance();
        Transformer tranformer;
        StringWriter writer = null;
        try {
            tranformer = tranFactory.newTransformer();
            writer = new StringWriter();
            Result result = new StreamResult(writer);
            Source source = new DOMSource(doc);
            tranformer.transform(source, result);
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(ServerAdapter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException tranEx) {
            tranEx.printStackTrace();
        }
        return writer.toString();
    }
}
